<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;
use DB;

class Mail extends Model
{
    protected $table = 'mail';
    protected $primaryKey = 'id';

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public static function getMailByAppid($appid,$status,$bind)
    {
        return self::where("appid",$appid)->where("status",$status)->where("bind",$bind)->first();
    }

    public static function insertAppid($id,$appid)
    {
        return self::where("appid",'')->where('id',$id)->where("status",1)->where("bind",0)->update(['appid'=>$appid]);
    }

    public static function getMailByAppidCount($appid)
    {
        return self::where("appid",$appid)->count();
    }

    public static function updateStatus($id,$appid,$status)
    {
        return self::where("id",$id)->where("appid",$appid)->update(['status'=>$status]);
    }

    public static function bindMail($id,$appid)
    {
        return self::where("id",$id)->where("appid",$appid)->update(['bind'=>1,'status'=>3]);
    }

    public static function createWorkQrcodeImg($kfid,$name,$img_url,$expiration_time,$count_limit,$rate)
    {
        return self::insert(["kfid"=>$kfid,"name"=>$name,"img_url"=>$img_url,"expiration_time"=>$expiration_time,"count_limit"=>$count_limit,"rate"=>$rate,"created_at"=>time()]);
    }

    public static function batchCreateWorkQrcodeImg($workQrcodeImg)
    {
        return self::insert($workQrcodeImg);
    }

    public static function getWorkQrcodeImgLists($kfid,$limit,$name,$sort,$order)
    {
        $query = self::select("*")->where("kfid",$kfid)->whereIn("status",[1,2]);
        if (!empty($name)) $query->where("name","like","%".$name."%");
        if(!empty($sort) && !empty($order)) $query->orderBy($sort,$order);
        return $query->orderBy("id","DESC")->paginate($limit);
    }

}
